/*
 * ResourceConfiguration.java
 *
 * Created on September 28, 2007, 9:06 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.www.util.script;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;

/**
 *
 * @author alex
 */
public class ResourceConfiguration
{

   List<Pattern> patterns;
   List<Properties> configurations;
   Properties defaults;
   
   /** Creates a new instance of ResourceConfiguration */
   public ResourceConfiguration()
   {
      this.defaults = new Properties();
      this.patterns = new ArrayList<Pattern>();
      this.configurations = new ArrayList<Properties>();
   }
   
   public void setDefaults(Properties config)
   {
      this.defaults = config;
   }
   
   public Properties getDefaults() 
   {
      return this.defaults;
   }
   
   public void addResource(String pattern,Properties config)
   {
      addResource(Pattern.compile(pattern),config);
   }
   
   public void addResource(Pattern pattern,Properties config)
   {
      patterns.add(pattern);
      configurations.add(config);
   }
   
   public Properties match(String path)
   {
      System.out.println("Matching: "+path);
      for (int i=0; i<patterns.size(); i++) {
         Pattern pattern = patterns.get(i);
         if (pattern.matcher(path).matches()) {
            System.out.println("Matched!");
            return configurations.get(i);
         }
      }
      return defaults;
   }
   
}
